<template>
  <view class="pay">
    <!-- 1 收货地址 -->
    <view class="address u-p-20">
      <view class="address-detail-loc" v-if="address">
        <view class="address-detail">{{ addressDetail }}</view>
        <view class="address-user"
          >{{ address.userName }} {{ encryptMobile }}</view
        >
      </view>
      <view class="address-btn u-flex u-row-center" v-else>
        <u-button
          shape="circle"
          type="error"
          plain
          size="medium"
          @click="handleChooseAddress"
          :custom-style="{ backgroundColor: '#fff!important' }"
          >获取收货地址</u-button
        >
      </view>
    </view>

    <u-line color="#eee"></u-line>

    <!-- 2 已选商品 -->
    <view
      class="goods-list u-p-15"
      v-for="item in checkedList"
      :key="item.goods_id"
    >
      <view class="goods-list-title">已选商品</view>
      <view class="goods-list-content">
        <view class="cart-item">
          <!-- 2 图片 -->
          <view class="goods-img">
            <u-image
              width="191rpx"
              height="191rpx"
              :src="item.goods_small_logo"
            ></u-image>
          </view>
          <!-- 3 信息 -->
          <view class="goods-info">
            <!-- 名称 -->
            <view class="goods-name u-line-2">{{ item.goods_name }}</view>
            <!-- 价格和数量 -->
            <view class="goods-price-num">
              <view class="goods-price">￥{{ item.goods_price }}</view>
              <view class="goods-num-tool">
                <view class="goods-vnum">{{ item.count }} </view>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 2 统计 -->
    <view class="statistics">
      <view class="all-price">
        合计: <text>￥{{ goodsTotalPrice }}</text>
      </view>
      <view class="all-count">
        <u-button type="error" shape="circle" @click="handlePay"
          >去支付(xxx)</u-button
        >
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data () {
    return {
      address: null
    }
  },
  computed: {
    ...mapGetters('cart', ['goodsTotalPrice', 'checkedList']),
    addressDetail () {
      if (this.address) {
        const { provinceName, cityName, countyName, detailInfo } = this.address
        return `${provinceName}${cityName}${countyName}${detailInfo}`
      } else {
        return ''
      }
    },
    encryptMobile () {
      if (this.address) {
        const { telNumber } = this.address
        return telNumber.replace(/^(\d{3})\d{4}(\d+)/, '$1****$2')
      } else {
        return ''
      }
    }
  },
  methods: {
    async handleChooseAddress () {
      const [err, res] = await uni.chooseAddress({})
      console.log(res)
      this.address = res
    },
    async handlePay () {
      // // 获取用户信息放前面，要在用户点击按钮时直接调用
      // const [error, { encryptedData, rawData, iv, signature }] = await uni.getUserProfile({ desc: '初始化' })
      // // console.log(result)
      // const [err, { code }] = await uni.login()
      // console.log(code)
      // const result123 = await this.$u.post('/users/wxlogin', { encryptedData, rawData, iv, signature, code: code })
      // console.log(result123)
      const token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjE3LCJpYXQiOjE2NTkxNjk4ODQsImV4cCI6MTAwMTY1OTE2OTg4M30.RvPQ-q5QwoB5_bSzFjU9ACjVDI-K42R7sOGVT0aChME'
      // 创建订单
      //  url / 请求头 token  / 请求体参数
      const orderParams = {
        order_price: this.goodsTotalPrice,
        consignee_addr: this.addressDetail,
        goods: this.checkedList.map(goods => {
          return {
            goods_id: goods.goods_id,
            goods_number: goods.count,
            goods_price: goods.goods_price
          }
        })
      }
      // 开始调用接口创建订单
      const result = await this.$u.post("/my/orders/create", orderParams, {
        Authorization: token,
      })
      // console.log(result);
      const { order_number } = result.message

      // 传入订单编号来获取支付参数
      const result2 = await this.$u.post(
        "/my/orders/req_unifiedorder",
        { order_number },
        {
          Authorization: token,
        }
      )
      // console.log(result2);
      const { pay } = result2.message
      console.log('pay', pay)
      // 调起微信支付
      const res11 = await uni.requestPayment({ provider: 'wxpay', ...pay })
      console.log(res11)
    }
  },
};
</script>

<style lang="scss">
.address-detail {
  padding: 15rpx;
  font-weight: 500;
  font-size: 18px;
  margin-bottom: 15rpx;
}
.address-user {
  padding: 15rpx;
}
.pay {
  padding-bottom: 111rpx;
}
.cart-item {
  display: flex;
  padding: 10rpx;
  border-bottom: 1rpx solid #ccc;

  .goods-img {
    flex: 2;

    display: flex;
    align-items: center;
    justify-content: center;
  }

  .goods-info {
    color: #666;
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    .goods-name {
    }

    .goods-price-num {
      display: flex;
      justify-content: space-between;
      .goods-price {
        color: #eb4450;
      }

      .goods-num-tool {
        width: 140rpx;
        height: 40rpx;

        display: flex;
        justify-content: flex-end;

        .goods-vnum {
        }
      }
    }
  }
}
.statistics {
  position: fixed;
  background-color: #fff;
  bottom: 0;
  left: 0;
  width: 100%;
  z-index: 100;
  border-top: 1rpx solid #ccc;
  display: flex;
  align-items: center;
  padding: 15rpx;
  .all-price {
    flex: 1;
    color: #666;
    text {
    }
  }

  .all-count {
  }
}
</style>